// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   WaveDataPCMShort.c
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>

#include "../WaveDataPCMShort.h"

/**
 *
 * @param file
 * @return
 */
WaveDataPCMShort* readWaveDataPCMShort(FILE* file) {
    WaveDataPCMShort* waveData = malloc(sizeof (WaveDataPCMShort));
    waveData->ckHeader = readChunkHeader(file);
    waveData->ckFormatPCM = readChunkFormatPCM(file);
    if (waveData->ckFormatPCM->format->wBitsPerSample != 16) {
        fprintf(stderr, "wBitsPerSample != 16\n");
        exit(EXIT_FAILURE);
    }
    waveData->ckDataPCMShort = readChunkDataPCMShort(file,
            waveData->ckFormatPCM->format->nChannels);
    return waveData;
}

/**
 *
 * @param waveData
 */
void freeWaveDataPCMShort(WaveDataPCMShort* waveData) {
    freeChunkHeader(waveData->ckHeader);
    freeChunkFormatPCM(waveData->ckFormatPCM);
    freeChunkDataPCMShort(waveData->ckDataPCMShort);
    free(waveData);
}

/**
 * 
 * @param waveData
 * @param file
 */
void writeWaveDataPCMShort(WaveDataPCMShort* waveData, FILE* file) {
    writeChunkHeader(waveData->ckHeader, file);
    writeChunkFormatPCM(waveData->ckFormatPCM, file);
    writeChunkDataPCMShort(waveData->ckDataPCMShort,
            waveData->ckFormatPCM->format->nChannels, file);
}
